<?php
class Request_a_quote extends Controller {
    
    function Request_a_quote(){
        
        parent::Controller();
        $this->load->database();
        $this->load->model('request_a_quote_model');
        $this->load->helper(array('form', 'url'));
		
	   //$this->output->enable_profiler(TRUE);
		$this->load->library('form_validation');
    }
	
	function index() 
	{
	
		$this->form_validation->set_error_delimiters('<div class="error" >','</div>');
		//$this->form_validation->set_rules('fullname','Fullname','required');
		$this->form_validation->set_rules('fullname','your fullname','callback_fullname');
		$this->form_validation->set_rules('company','your company name','callback_company');
		$this->form_validation->set_rules('telephone','your contact number','required');
		$this->form_validation->set_rules('email','e-mail address','required|valid_email');
		$this->form_validation->set_rules('job_title','a job name or number','callback_job_title');
		//$this->form_validation->set_rules('colours','Colours','required');
		$this->form_validation->set_rules('paper_size','a paper size','callback_paper_size');
		$this->form_validation->set_rules('paper','a paper name or type','callback_paper');
		$this->form_validation->set_rules('quantity','a print quantiy, or multiple','required|numeric');
		//$this->form_validation->set_rules('finishing','Finishing','callback_finishing');
		$this->form_validation->set_message('required', 'Please enter %s');
		$this->form_validation->set_message('valid_email', 'Please enter  %s');
		$this->form_validation->set_message('numeric', 'Please enter  %s  ');
	
		if ($this->form_validation->run() == FALSE)
		{
			
		   $list_paper_colour= $this->request_a_quote_model->list_quotes_paper_data('bur_papercolours');
           $list_paper_size= $this->request_a_quote_model->list_quotes_paper_data('bur_papersizes');
           $list_paper_name= $this->request_a_quote_model->list_quotes_paper_data('bur_papers');
          
           $data['paper_colour']=$list_paper_colour;
           $data['paper_size']=$list_paper_size;
           $data['paper_name']=$list_paper_name;
         
			$this->load->view(TEMPLATE_NAME.'/front/request_a_quote/request_a_quote_view',$data);
		}
		else
		{
		 
			$fullname   =$this->input->post('fullname');
			$companyname=$this->input->post('company');	
			$telephone  =$this->input->post('telephone');	
			$email      =$this->input->post('email');	
			$jobtitle    =$this->input->post('job_title');	
			$papercolour_id=$this->input->post('colours');	
			//$papersize_id  =$this->input->post('paper_size');
			$papersize_id  =$this->input->post('paper_sizeid');
			
			$papersize_width=$this->input->post('papersize_width');
			$papersize_height=$this->input->post('papersize_height');
			$papername = $this->input->post('paper');	
			$paperid = $this->input->post('paperidvals');	
			$quantity     =$this->input->post('quantity');	
			$finishing   =$this->input->post('finishing');	
		 //searches for all the line breaks or carriage returns within a string and replace them with line break tag or <br> tag.
		   $finishing=nl2br($finishing); 
      
			if($papersize_id=='0')
            {
               $paper_size='custom '.$papersize_width.' x '.$papersize_height.' mm';   
                 
            }else{
               
               $papersize_width='0';
               $papersize_height='0';
            
               $paper_size=$this->request_a_quote_model->get_papersize_format($papersize_id);
                  
            }   
             
             $paper_colour=$this->request_a_quote_model->get_data_colour($papercolour_id);
             // $paper_size=$this->request_a_quote_model->get_papersize_format($papersize_id);
             //$paper_name=$this->request_a_quote_model->get_papername_format($papername_id);

             $paper_name = $papername;
             //upload artwork
            $artwork=$this->do_upload();
            //print_r($artwork);die()
             
         	if($artwork==''){    
			$artwork='none';
			}
           
		 $artwork_size=$this->artwork_Size($artwork);
		 
		
		 
		// print_r($artwork_size);die();
			//requestaquote_insert
			
		$data_quote = array("quote_fullname"=>$fullname,
						"quote_companyname"=>$companyname,	
						"quote_telephone"=>$telephone,	
						"quote_email"=>$email,	
						"quote_jobtitle"=>$jobtitle,	
						"quote_papercolourid"=>$papercolour_id,	
						"quote_papersizeid"=>$papersize_id,
						"quote_paperwidth"=>$papersize_width,	
						"quote_paperheight"=>$papersize_height,	
						"quote_papername"=>$papername,
						"quote_paperid"	=> $paperid,
						"quote_quantity"=>$quantity,	
						"quote_finishing"=>$finishing,	
						"quote_artwork"=>$artwork,
						"quote_artwork_size"=>$artwork_size,
						"quote_date"=>date('Y-m-d H:i:s')
					
					);
		/* $this->request_a_quote_model->requestaquote_insert($fullname,$companyname,$telephone,$email,$jobtitle,$papercolour_id
       ,$papersize_id,$papersize_width,$papersize_height,$papername_id,$quantity,$finishing,$artwork,$artwork_size);*/
           
		   $this->request_a_quote_model->requestaquote_insert($data_quote);
		  
             //request_a_quote_email_admin
            $this->request_a_quote_model->send_request_a_quote_email_admin(
            $fullname,$companyname,$telephone,$email,$jobtitle,
            $paper_colour,$paper_size,'','',$paper_name,$quantity,$finishing,$artwork,$artwork_size);
            
            //request_a_quote_email_client
            $this->request_a_quote_model->send_request_a_quote_email_client($fullname,$companyname,$telephone,$email,$jobtitle,
            $paper_colour,$paper_size,'','',$paper_name,$quantity,$finishing,$artwork,$artwork_size);
			
		redirect('request_a_quote/success');
			
			
		
		}
	}
    
    function success(){
        
      	$this->load->view(TEMPLATE_NAME.'/front/request_a_quote/request_a_quote_success'); 
    }
    
    function artwork_Size($artwork){
    
	$artwork_size = '';
     if($artwork=='none'){
			$artwork='none';
            return $artwork_size='';
			}
			else{
				
                $artwork_size.=number_format($_FILES["userfile"]["size"]/1024000 ,3);
               
                return $artwork_size;
				}
    
  }
	
	function fullname($str)
	{
		if($str == '')
		{
		$this->form_validation->set_message('fullname', 'Please enter %s ');
			return FALSE;
		}
		elseif ($str == 'Your Name')
		{
			$this->form_validation->set_message('fullname', 'Please enter %s ');
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	}
    
    function paper_size($str)
	{
	    $papersize_width=$this->input->post('papersize_width');
        $papersize_height=$this->input->post('papersize_height');
        
		if($str == '')
		{
			$this->form_validation->set_message('paper_size', 'Please enter %s ');
			return FALSE;
		}
		/*elseif ($papersize_width=='' || $papersize_height='' || !is_numeric($papersize_width) || !is_numeric($papersize_height))
		{
			$this->form_validation->set_message('paper_size', 'Please enter paper width and height');
			return FALSE;
		}*/
		else
		{
			return TRUE;
		}
	}
	
	function company($str)
	{
		if($str == '')
		{
		$this->form_validation->set_message('company', 'Please enter %s ');
			return FALSE;
		}
		elseif ($str == 'your company name')
		{
			$this->form_validation->set_message('company', 'Please enter %s ');
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	}
	
	function paper($str)
	{
		if($str == '')
		{
			$this->form_validation->set_message('paper', 'Please enter %s ');
			return FALSE;
		}
		elseif ($str == 'No records found')
		{
			$this->form_validation->set_message('paper', 'Please enter %s ');
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	}
	function job_title($str)
	{
		if($str == '')
		{
		$this->form_validation->set_message('job_title', 'Please enter %s ');
			return FALSE;
		}
		elseif ($str == 'your job title / number')
		{
			$this->form_validation->set_message('job_title', 'Please enter %s ');
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	}
	
	function finishing($str)
	{
		if($str == '')
		{
		$this->form_validation->set_message('job_title', 'Please enter %s ');
			return FALSE;
		}
		elseif ($str == 'extra details')
		{
			$this->form_validation->set_message('job_title', 'Please enter %s ');
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	}
	
	
	
	function do_upload()
	{
		
		$config['upload_path'] ='./quote_upload/';
		$config['allowed_types'] = 'pdf|gif|jpg|png';
		$config['max_size']	= '200048';
		//$config['max_width']  = '1024';
		//$config['max_height']  = '768';
		
		$this->load->library('upload', $config);
       
		if ( ! $this->upload->do_upload())
		{
			$error = array('error' => $this->upload->display_errors());
			
		
			//$this->load->view('request_a_quote_view');
		}	
		else
		{
			
			$data = array('upload_data' => $this->upload->data());
		
	return  $data['upload_data']['file_name'];
           // echo $data['upload_data']['client_name'];
          //  die();
		//	return true;
					}
                  
	}	
	function autocomplete()
	{
		$result = array();
		
		
		$q = $this->input->xss_clean($this->input->post('term'));
		if (!$q) return;
		$items = $this->request_a_quote_model->lookup($q); 
		
		echo $this->array_to_json($items);
		
	}
	
	function autocomplete_size()
	{
		$result = array();
		
		
		$q = $this->input->xss_clean($this->input->post('term'));
		if (!$q) return;
		$items = $this->request_a_quote_model->lookup_size($q); 
		
		echo $this->array_to_json($items);
	}
	
	function array_to_json( $array )
	{

		if( !is_array( $array ) ){
			return false;
		}
	
		$associative = count( array_diff( array_keys($array), array_keys( array_keys( $array )) ));
		if( $associative ){
	
			$construct = array();
			foreach( $array as $key => $value ){
	
				// We first copy each key/value pair into a staging array,
				// formatting each key and value properly as we go.
	
				// Format the key:
				if( is_numeric($key) ){
					$key = "key_$key";
				}
				$key = "\"".addslashes($key)."\"";
	
				// Format the value:
				if( is_array( $value )){
					$value = $this->array_to_json( $value );
				} else if( !is_numeric( $value ) || is_string( $value ) ){
					$value = "\"".addslashes($value)."\"";
				}
	
				// Add to staging array:
				$construct[] = "$key: $value";
			}
	
			// Then we collapse the staging array into the JSON form:
			$result = "{ " . implode( ", ", $construct ) . " }";
	
		} else { // If the array is a vector (not associative):
	
			$construct = array();
			foreach( $array as $value ){
	
				// Format the value:
				if( is_array( $value )){
					$value = $this->array_to_json( $value );
				} else if( !is_numeric( $value ) || is_string( $value ) ){
					$value = "'".addslashes($value)."'";
				}
	
				// Add to staging array:
				$construct[] = $value;
			}
	
			// Then we collapse the staging array into the JSON form:
			$result = "[ " . implode( ", ", $construct ) . " ]";
		}
	
		return $result;
	}
	
}
?>